/******************************************************
 * @file main.c
 * @author Destiny 
 * @brief 初步实现了以下功能：
 * 电子书阅读、鼠标与触摸屏输入、文件浏览、相机与相册、手写数字识别、基于MQTT协议的简易物联网
 * @version 0.1
 * @date 2024-07-18
 *****************************************************/
/* Includes ------------------------------------------------------------------*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <pthread.h>
/* Private Includes ----------------------------------------------------------*/
#include <config.h>
#include <encoding_manager.h>
#include <fonts_manager.h>
#include <disp_manager.h>
#include <input_manager.h>
#include <pic_operation.h>
#include <render.h>
#include <picfmt_manager.h>
#include <video_manager.h>
#include <mqttclient.h>
#include <model_manager.h>
#include <recv.h>

/* Private Define ------------------------------------------------------------*/

/* Extern variables ---------------------------------------------------------*/
extern int g_iReaderFontSize;
extern char g_cFontsFile[16];

/* Private Function Prototypes -----------------------------------------------*/

/* Public Function Prototypes ------------------------------------------------*/
/**
 * @brief main函数
 * 
*/
int main(int argc, char **argv)
{	
	int iError;
	pthread_t MqttThreadId;
	pthread_t ParseMsgThreadId;

	/*调试通道初始化*/
	DebugInit();
	InitDebugChanel();

	/*显示设备初始化*/
	iError = DisplayInit();
	if (iError)
	{
		printf("DisplayInit error!\n");
	}
	SelectAndInitDefaultDispDev("fb");

	/*分配显存*/
	iError = AllocVideoMem(5);
	if (iError)
	{
		printf("AllocVideoMem error!\n");
	}

	/*输入设备注册与初始化（触摸屏、鼠标）*/
	iError = InputInit();
	if (iError)
	{
		printf("InputInit error!\n");
	}
	iError = AllInputDevicesInit();
	if (iError)
	{
		printf("AllInputDevicesInit error!\n");
	}

	/*编码模块初始化*/
	iError = EncodingInit();
	if (iError)
	{
		printf("EncodingInit error!\n");
	}

	/*Video初始化*/
	iError = VideoInit();
	if (iError)
	{
		printf("VideoInit error!\n");
	}

	/*字体模块初始化设置*/
	iError = FontsInit();
	if (iError)
	{
		printf("FontsInit error!\n");
	}
	iError = SetFontsDetail(FONT_LIB_NAME, g_cFontsFile, g_iReaderFontSize);
	if (iError)
	{
		printf("SetFontsDetail error!\n");
	}

	/*图片解码模块*/
    iError = PicFmtsInit();
	if (iError)
	{
		printf("PicFmtsInit error!\n");
	}

	/*页面UI*/
	iError = PagesInit();
	if (iError)
	{
		printf("PagesInit error!\n");
	}

	/*神经网络模型*/
	iError = ModelInit();
	if (iError)
	{
		printf("ModelInit error!\n");
	}

	/*创建mqtt线程*/
	iError = pthread_create(&MqttThreadId, NULL, Mqtt, NULL);
	if (iError)
	{
		printf("pthread_create error!\n");
	}	

	/*消息处理线程*/		
	iError = pthread_create(&ParseMsgThreadId, NULL, ParseMsg, NULL);
	if (iError)
	{
		printf("pthread_create error!\n");
	}
	
	/*运行主页面*/
	Page("main")->Run(NULL);

	return 0;
}

